import { getCurrentInstance } from 'vue';
import { IResultOr } from '@/api/interface';
import { userSignApi, userLoginApi } from '@/api/login';
import { useStore } from '@/store';
import { useRoute } from 'vue-router';

interface IRuleForm {
  mobile: string;
  password: string;
}
export default function useFormProperties(router: any, params: any) {
  const { proxy }: any = getCurrentInstance();
  const store = useStore();
  const route = useRoute();
  console.log(store);
  // 注册接口
  function userSign(params: IRuleForm) {
    userSignApi(params).then((res: IResultOr) => {
      const { success, message } = res;
      if (success) {
        proxy.$message.success(message);
      } else {
        proxy.$message.error(message);
      }
    });
  }
  // 登录接口
  function userLogin(params: IRuleForm) {
    userLoginApi(params).then((res: IResultOr) => {
      const { success, message, result } = res;
      if (success) {
        let { status, userId } = result;
        localStorage.setItem('userId', userId);
        store.commit('setUserStatus', status);
        const { redirect }: any = route.query;
        console.log(redirect);
        router.push({ path: redirect || '/' });
        //window.location.href = '/home';
        proxy.$message.success(message);
      } else {
        proxy.$message.error(message);
      }
    });
  }
  return {
    proxy,
    userSign,
    userLogin,
  };
}
